package cn.zhejianglao.shirotemplate.service.user;

import cn.zhejianglao.shirotemplate.common.model.Response;

import cn.zhejianglao.shirotemplate.common.model.UserRoleGetOutDTO;
import cn.zhejianglao.shirotemplate.exception.BusinessException;
import cn.zhejianglao.shirotemplate.exception.ExceptionCode;
import cn.zhejianglao.shirotemplate.service.user.model.CreateUserRequest;
import cn.zhejianglao.shirotemplate.service.user.model.LoginRequest;
import cn.zhejianglao.shirotemplate.service.user.model.UserGetOutDTO;
import cn.zhejianglao.shirotemplate.service.user.service.IUserService;
import cn.zhejianglao.shirotemplate.common.validator.ParamValidator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public Response<String> login(@RequestBody LoginRequest request) {
        ParamValidator.validate(request);
        try {
            userService.login(request);
            return Response.success("success");
        } catch (IncorrectCredentialsException e) {
            log.info("wrong password");
            throw new BusinessException(
                    ExceptionCode.Permission.AUTHENTICATION_INCORRECT_PASSWORD_CODE,
                    ExceptionCode.Permission.AUTHENTICATION_INCORRECT_PASSWORD_MSG);
        } catch (Exception e) {
            log.error("error:", e);
            throw new BusinessException(
                    ExceptionCode.Permission.AUTHENTICATION_FAILURE_CODE,
                    ExceptionCode.Permission.AUTHENTICATION_FAILURE_MSG);
        }
    }

    @RequiresRoles("admin")
    @PostMapping("/create")
    public Response<String> create(@RequestBody CreateUserRequest request) {
        ParamValidator.validate(request);
        String result = userService.create(request);
        return Response.success(result);
    }

    @GetMapping("/current")
    public Response<UserGetOutDTO> current() {
        UserGetOutDTO user = userService.getCurrentUser();
        return Response.success(user);
    }

    @GetMapping("/roles")
    public Response<List<UserRoleGetOutDTO>> roles() {
        return Response.success(userService.getRoles());
    }

    @GetMapping("/switchRole/{roleId}")
    public Response<String> switchRole(@PathVariable("roleId") Long roleId) {
        userService.switchRole(roleId);
        return Response.success();
    }

    @GetMapping("/rename/{newName}")
    public Response<String> rename(@PathVariable("newName") String newName) {
        if (StringUtils.isBlank(newName)) {
            throw new BusinessException(
                    ExceptionCode.Param.NULL_PARAM_CODE,
                    ExceptionCode.Param.NULL_PARAM_MSG
            );
        }
        userService.rename(newName);
        return Response.success();
    }

    @RequiresRoles("admin")
    @DeleteMapping("/delete/{id}")
    public Response<String> delete(@PathVariable("id") Long userId) {
        if (userId == null) {
            throw new BusinessException(
                    ExceptionCode.Param.NULL_PARAM_CODE,
                    ExceptionCode.Param.NULL_PARAM_MSG
            );
        }
        userService.delete(userId);
        return Response.success();
    }
}
